package com.fun.demo.fun.design;

import java.util.ArrayList;
import java.util.List;

//将对象组合成树形结构以表示“部分-整体”的层次结构
public class Composite {
}


interface Employee {
    void add(Employee employee);
    void remove(Employee employee);
    Employee getChild(int i);
    String getName();
    double getSalary();
    void printStructure();
}

class Developer implements Employee {
    private String name;
    private double salary;

    public Developer(String name, double salary) {
        this.name = name;
        this.salary = salary;
    }

    @Override
    public void add(Employee employee) {
        // This is leaf node so this method should not be implemented
    }

    @Override
    public void remove(Employee employee) {
        // This is leaf node so this method should not be implemented
    }

    @Override
    public Employee getChild(int i) {
        // This is leaf node so this method should not be implemented
        return null;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public double getSalary() {
        return salary;
    }

    @Override
    public void printStructure() {
        System.out.println("Developer: Name: " + getName() + ", Salary: " + getSalary());
    }
}

class Manager implements Employee {
    private String name;
    private double salary;
    private List<Employee> employees = new ArrayList<>();

    public Manager(String name, double salary) {
        this.name = name;
        this.salary = salary;
    }

    @Override
    public void add(Employee employee) {
        employees.add(employee);
    }

    @Override
    public void remove(Employee employee) {
        employees.remove(employee);
    }

    @Override
    public Employee getChild(int i) {
        return employees.get(i);
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public double getSalary() {
        return salary;
    }

    @Override
    public void printStructure() {
        System.out.println("Manager: Name: " + getName() + ", Salary: " + getSalary());
        for (Employee employee : employees) {
            employee.printStructure();
        }
    }
}

